﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using seiacpscs_a2.datastructures.hashtable;
using seiacpscs_a2.datastructures.linkedlist;
using seiacpscs_a2.utilities;

namespace seiacpscs_a2_test.datastructures.hashtable
{
    [TestClass]
    public class HashTableTestCase
    {
    
    [TestMethod]
	public void testHashTable() {
					
		int [] items = new int[6];
		
		items[0] = 0 ; items[1] = 1 ; items[2] = 2;
		items[3] = 10; items[4] = 11;
		items[5] = 20;
		
		HashTableStrategy strategy = new ModuloStrategy();		
		HashTable table = new HashTable(items,strategy);
		
		Assert.AreEqual(10, table.NumberOfBuckets);
		
		LinkedList bucket0 =table.getBucket(0);
		Assert.AreEqual(3, bucket0.Length);
		Assert.IsTrue(bucket0.contains(0));
		Assert.IsTrue(bucket0.contains(10));
		Assert.IsTrue(bucket0.contains(20));
		Assert.IsFalse(bucket0.contains(11));
				
		LinkedList bucket1 = table.getBucket(1);
		Assert.AreEqual(2, bucket1.Length);
		Assert.IsTrue(bucket1.contains(1));
		Assert.IsTrue(bucket1.contains(11));
		Assert.IsFalse(bucket1.contains(10));
		
		LinkedList bucket2 =table.getBucket(2);
		Assert.AreEqual(1, bucket2.Length);
		Assert.IsTrue(bucket2.contains(2));
		Assert.IsFalse(bucket2.contains(10));
		
		LinkedList bucket3 =table.getBucket(3);
		Assert.AreEqual(0, bucket3.Length);
		
		LinkedList bucket4 =table.getBucket(4);
		Assert.AreEqual(0, bucket4.Length);
		
		LinkedList bucket5 =table.getBucket(5);
		Assert.AreEqual(0, bucket5.Length);
		
		LinkedList bucket6 =table.getBucket(6);
		Assert.AreEqual(0, bucket6.Length);
		
		LinkedList bucket7 =table.getBucket(7);
		Assert.AreEqual(0, bucket7.Length);
		
		LinkedList bucket8 =table.getBucket(8);
		Assert.AreEqual(0, bucket3.Length);
		
		LinkedList bucket9 =table.getBucket(9);
		Assert.AreEqual(0, bucket9.Length);
				
	}

    
    [TestMethod]
	public void testContains(){
		
		int [] items = new int[6];
		
		items[0] = 0 ; items[1] = 1 ; items[2] = 2;
		items[3] = 10; items[4] = 11;
		items[5] = 20;
			
		HashTableStrategy strategy = new ModuloStrategy();		
		HashTable table = new HashTable(items,strategy);
		
		// 1st bucket
		Assert.IsTrue(table.contains(0));
		Assert.AreEqual(1,table.NumberOfAccesses);	
		Assert.IsTrue(table.contains(10));
		Assert.AreEqual(2,table.NumberOfAccesses);
		Assert.IsTrue(table.contains(20));
		Assert.AreEqual(3,table.NumberOfAccesses);
		
		// 2nd bucket
		Assert.IsTrue(table.contains(1));
		Assert.AreEqual(1,table.NumberOfAccesses);		
		Assert.IsTrue(table.contains(11));
		Assert.AreEqual(2,table.NumberOfAccesses);
		
		// 3rd bucket
		Assert.IsTrue(table.contains(2));
		Assert.AreEqual(1,table.NumberOfAccesses);
				
		// Negative Testing for Contains
		Assert.IsFalse(table.contains(30));
		Assert.AreEqual(3,table.NumberOfAccesses);
		
		Assert.IsFalse(table.contains(21));
		Assert.AreEqual(2,table.NumberOfAccesses);
		
		Assert.IsFalse(table.contains(22));
		Assert.AreEqual(1,table.NumberOfAccesses);
		
		Assert.IsFalse(table.contains(25));
		Assert.AreEqual(0,table.NumberOfAccesses);
		
	}

    [TestMethod]
	public void testHashTableWithLargeNumberOfItems(){
		
		int [] items;
		String inputFile = @"C:\comp6411_files\ht_inputfile_1000Items_forUnitTesting.txt";
		
		FileParser parser = new FileParser(inputFile);	
		
		items = parser.GetNumbers();
		HashTableStrategy strategy = new ModuloStrategy();		
		HashTable table = new HashTable(items,strategy);
			  
		Assert.AreEqual(1000, items.Length);
		Assert.AreEqual(1000, table.Size);
				  
	}

    [TestMethod]
	public void testHashTableWithReflexiveStrategy(){
		
		int [] items;
		String inputFile = @"C:\comp6411_files\ht_inputfile_1000Items_forUnitTesting.txt";	
		
		FileParser parser = new FileParser(inputFile);	
		
		items = parser.GetNumbers();
		HashTableStrategy strategy = new ReflexiveStrategy();		
		HashTable table = new HashTable(items,strategy);
			  
		Assert.AreEqual(1000, items.Length);
		Assert.AreEqual(1000, table.Size);
			  
		for (int i =0; i< table.buckets.Length;i++)
		{
			LinkedList bucket = table.getBucket(i);
			Assert.IsTrue(bucket.Length <= 1);
		}
		
	}
	
	
    }
}
